package com.yj.ccs.abs.web.asset.mapper;

import com.yj.ccs.abs.web.asset.entity.FundRestream;
import com.yj.ccs.abs.web.asset.entity.FundRestreamListItem;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/22
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface FundRestreamMapper {

    @Options(useGeneratedKeys = true, keyProperty = "item.id")
    @Insert("INSERT INTO `fund_restream` (asset_id, date, money, type, note_money, expire_date, source, create_time) " +
            "VALUES (#{item.asset_id}, #{item.date}, #{item.money}, #{item.type}, #{item.note_money}, " +
            "#{item.expire_date}, #{item.source}, NOW())")
    void create(@Param("item") FundRestream item);

    @Insert("<script>" +
            "INSERT INTO `fund_restream` (asset_id, date, money, type, note_money, expire_date, source, create_time) " +
            "VALUES " +
            "<foreach collection = 'list' item = 'item' separator = ', '>" +
            "(#{item.asset_id}, #{item.date}, #{item.money}, #{item.type}, #{item.note_money}, #{item.expire_date}, #{item.source}, NOW())" +
            "</foreach>" +
            "</script>")
    void batchInsert(@Param("list") List<FundRestream> list);

    @Update("UPDATE `fund_restream` SET date = #{item.date}, money = #{item.money}, type = #{item.type}, " +
            "note_money = #{item.note_money}, expire_date = #{item.expire_date} " +
            "WHERE id = #{item.id}")
    void update(@Param("item") FundRestream item);

    @Delete("DELETE FROM `fund_restream` WHERE id = #{id}")
    void deleteById(@Param("id") Long id);

    @Select("SELECT * FROM `fund_restream` WHERE id = #{id} LIMIT 1")
    FundRestream getById(@Param("id") Long id);

    @Select("SELECT id, date, source, money, type, note_money, expire_date FROM `fund_restream` WHERE asset_id = #{asset_id}")
    List<FundRestreamListItem> getListItemByAssetId(@Param("asset_id") Long asset_id);

    @Select("SELECT SUM(money) FROM `fund_restream` WHERE asset_id = #{asset_id}")
    BigDecimal countSumMoneyForAssetId(@Param("asset_id") Long asset_id);

    @Select("SELECT SUM(note_money) FROM `fund_restream` WHERE asset_id = #{asset_id}")
    BigDecimal countSumNoteMoneyForAssetId(@Param("asset_id") Long asset_id);
}
